package com.cat.dataStructure02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimum-speed-to-arrive-on-time/
 * @create 2025/10/13 20:19
 * @since JDK17
 */

public class Solution14 {
    boolean check(int[] dist, int speed, double hour) {
        double time = 0;
        int n = dist.length - 1;
        for (int i = 0; i < n; i++) {
            time += Math.ceil((double)dist[i] / speed);
        }
        return time + (double)dist[n] / speed <= hour;
    }
    public int minSpeedOnTime(int[] dist, double hour) {
        int l = -1, r = ((int) 1e8), mid;
        int ans = -1;
        while (l + 1 < r) {
            mid = l + r >> 1;
            if (check(dist, mid, hour)) {
                r = mid;
                ans = mid;
            } else {
                l = mid;
            }
        }
        return ans;
    }
}
